﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OkTool.Util
{
    public class ValidatorUtil
    {
        /// <summary>
        /// 验证是否为生日
        /// <list type="table">
        /// <item>只支持以下几种格式</item>
        /// <item>yyyyMMdd</item>
        /// <item>yyyy-MM-dd</item>
        /// <item>yyyy/MM/dd</item>
        /// <item>yyyy.MM.dd</item>
        /// <item>yyy年MM月dd日</item>
        /// </list>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsBirthday(string value)
        {
           var match= RegexUtil.Match(value, RegexUtil.BIRTHDAY);
            if (match.Success)
            {
                int year = int.Parse(match.Groups[1].Value);  
                int month = int.Parse(match.Groups[3].Value);
                int day = int.Parse(match.Groups[5].Value);
                return IsBirthday(year, month, day);  
            }
            return false;
        }

        /// <summary>
        /// 验证是否为生日
        /// </summary>
        /// <param name="year">年,从1900年开始计算</param>
        /// <param name="month">月,从1开始计算</param>
        /// <param name="day">日,从1开始计算</param>
        /// <returns></returns>
        public static bool IsBirthday(int year, int month, int day)
        {
            //验证年
            int thisYear = DateUtil.ThisYear();
            if (year < 1900 || year > thisYear)
            {
                return false;
            }

            //验证月

            if (month < 1 || month > 12)
            {
                return false;
            }

            //验证日
            if (day < 1 || day > 31)
            {
                return false;
            }

            //检查几个特殊月的最大天数

            if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11))
            {
                return false;
            }

            if (month == 2)
            {
                //在2月,非闰年最大28,闰年最大29
                return day < 29 || (day == 29 && DateUtil.IsLeapYear(year));
            }
            return true;

        }
    }
}
